/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.Allocation;
import com.openbrew.openbrewrf.AllocationTemplate;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import model.InsertedAllocations;
import tools.AllocationTools;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.allocation")
public class AllocationFacadeREST extends AbstractFacade<Allocation> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public AllocationFacadeREST() {
        super(Allocation.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public Allocation create(@WebParam(name = "allocation") Allocation entity) {
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Path("updateAllSimilarItems/{daysChange}")
    @Consumes({"application/xml", "application/json"})
    @Produces(MediaType.APPLICATION_XML)
    public InsertedAllocations editAllSimilar(Allocation entity, @PathParam("daysChange") Integer daysChanged) {
        super.edit(entity);
        em.getEntityManagerFactory().getCache().evictAll();
        final AllocationTools at = new AllocationTools();
        InsertedAllocations ia = new InsertedAllocations();
        ia.setAllocations(at.updateAllSimilarAllocations(em, entity, daysChanged));
        em.getEntityManagerFactory().getCache().evictAll();
        return ia;
    }

    @PUT
    @Path("setAllocationCompleteAndUpdateAllocations")
    @Consumes({"application/xml", "application/json"})
    public void setAllocationCompleteAndUpdateAllocations(Allocation entity) {
        entity.setAllocationFilled(true);
        final AllocationTools at = new AllocationTools();
        at.checkAndInsertAllocation(em, entity.getTemplateID(), entity.getAllocationDueDate());
        super.edit(entity);
        em.getEntityManagerFactory().getCache().evictAll();
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Allocation entity) {
        super.edit(entity);
        em.getEntityManagerFactory().getCache().evictAll();
    }

    @PUT
    @Path("updateTemplateFromAllocation/{id}")
    @Consumes({"application/xml", "application/json"})
    public void updateAllocationTemplateFromAllocation(Allocation entity, @PathParam("id") Integer templateID) {
        final AllocationTools at = new AllocationTools();
        at.updateTemplateFromAllocation(em, templateID, entity);
    }

    @PUT
    @Path("createPSOFromAllocation/{custID}")
    @Consumes({"application/xml", "application/json"})
    public void createPSOFromAllocation(Allocation entity, @PathParam("custID") Integer customerID) {
        final AllocationTools at = new AllocationTools();
        at.createPSOFromAllocation(em, entity, customerID);
    }

    @POST
    @Path("createNewTemplateFromAllocation/{templateName}/{repType}/{repCount}")
    @Consumes({"application/xml", "application/json"})
    public AllocationTemplate createNewTemplateFromAllocation(@WebParam(name = "allocation") Allocation entity
            , @PathParam("templateName") String description, @PathParam("repType") Integer repType
            , @PathParam("repCount") Integer repCount) {
        final AllocationTools at = new AllocationTools();
        return at.createNewTemplateFromAllocation(em, description, entity, repType, repCount);
    }

    @GET
    @Path("insertAllocationFromTemplate/{id}/{dueDate}")
    @Consumes({"application/xml", "application/json"})
    @Produces(MediaType.APPLICATION_XML)
    public InsertedAllocations insertAllocationFromTemplate(@PathParam("id") Integer templateID, @PathParam("dueDate") Date dueDate) {
        final AllocationTools at = new AllocationTools();
        InsertedAllocations ia = new InsertedAllocations();
        ia.setAllocations(at.checkAndInsertAllocation(em, templateID, dueDate));
        return ia;
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
        em.getEntityManagerFactory().getCache().evictAll();
    }

    @DELETE
    @Path("removeAllSimilarAfterDate/{templateID}/{date}")
    public void removeAllSimilarAfterDate(@PathParam("templateID") Integer id, @PathParam("date") Date date) {
        final AllocationTools at = new AllocationTools();
        at.removeAllSimilarAllocationsFromDate(em, date, id);
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Allocation find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Allocation> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Allocation> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }
    
    @GET
    @Path("runNamedQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Allocation> runNamedQuery(@PathParam("str") String str){
        return super.runNamedQuery(str);
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
